home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / ctutor.zip / CHAP14.TXT < prev    next >
Text File  |  1987-07-04  |  15KB  |  322 lines

  1.                      Chapter 14 - Example Programs
  2.  
  3.  
  4.                              WHY THIS CHAPTER?
  5.  
  6.              Although  every  program  in this tutorial has  been  a
  7.         complete  program,  each  one  has also been  a  very  small
  8.         program intended to teach you some principle of  programming
  9.         in  C.   It  would do you a disservice to leave you at  that
  10.         point  without introducing you to a few larger  programs  to
  11.         illustrate  how  to  put together the  constructs  you  have
  12.         learned  to create a major program.   This chapter  contains
  13.         four  programs  of increasing complexity,  each designed  to
  14.         take  you  into a higher plateau of  programming,  and  each
  15.         designed to be useful to you in some way.
  16.  
  17.              DOSEX will illustrate how to make DOS system calls  and
  18.         will teach you,  through self-study, how the system responds
  19.         to  the  keyboard.   WHATNEXT  reads commands input  on  the
  20.         command line and will aid you in setting up a variable batch
  21.         file,  one  that requests an operator input and responds  to
  22.         the  input  by branching to a different part  of  the  batch
  23.         file.
  24.  
  25.              LIST  is  the source code for the program you  used  to
  26.         print  out the C source files when you began studying C with
  27.         the aid of this tutorial.  Finally we come to VC, the Visual
  28.         Calculator,  which  you should find to be a  useful  program
  29.         even  if you don't study its source code.   VC uses most  of
  30.         the  programming  techniques we have studied in this  course
  31.         and  a few that we never even mentioned such  as  separately
  32.         compiled subroutines.
  33.  
  34.              We  will  take a look at the example programs one at  a
  35.         time  but  without  a complete explanation of  any  of  them
  36.         because  you  have  been studying C for some  time  now  and
  37.         should be able to read and understand most of these programs
  38.         on  your  own.
  39.  
  40.                      DOSEX.C - The DOS Example Program
  41.  
  42.              The  copy of DOS that you received with your IBM-PC  or
  43.         compatible has about 50 internal DOS calls that you can  use
  44.         as  a programmer to control your peripheral devices and read
  45.         information  or status from them.   Some of the earlier  IBM
  46.         DOS manuals, DOS 2.0 and earlier, have these calls listed in
  47.         the back of the manual along with how to use them.   Most of
  48.         the  manuals  supplied  with compatible  computers  make  no
  49.         mention  of  these  calls even  though  they  are  extremely
  50.         useful.   These  calls  can  be  accessed  from  nearly  any
  51.         programming  language but they do require some initial study
  52.         to learn how to use them.   This program is intended to  aid
  53.         you in this study.
  54.  
  55.  
  56.  
  57.                                   Page 97
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                      Chapter 14 - Example Programs
  68.  
  69.  
  70.              Display the program on your monitor or print it out for
  71.         reference.   It  is  merely a loop watching for  a  keyboard
  72.         input or a change in the time.  If either happens, it reacts
  73.         accordingly.   In line 32,  the function "kbhit()" returns a
  74.         value  of 1 if a key has been hit but not yet read from  the
  75.         input buffer by the program.
  76.  
  77.              Look at the function named "get_time" for an example of
  78.         a  DOS call.   An interrupt 21(hex) is called after  setting
  79.         the  AH  register to 2C(hex) =  44(decimal).   The  time  is
  80.         returned in the CH,  CL, and DH registers.  Refer to the DOS
  81.         call  definitions in your copy of DOS.   If the  definitions
  82.         are  not included there,  Peter Nortons  book,  "Programmers
  83.         Guide  to  the  IBM PC" is recommended as a  good  reference
  84.         manual   for   these  calls  and  many   other   programming
  85.         techniques.   Note  that  Turbo  C  has  a  function   named
  86.         "gettime"  that does the same thing.  You should spend  some
  87.         time  studying the Turbo C Reference Guide to learn  of  the
  88.         availability of such functions.
  89.  
  90.              Another useful function is the "pos_cursor()"  function
  91.         that  positions the cursor anywhere on the monitor that  you
  92.         desire  by  using  a  DOS  interrupt.   In  this  case,  the
  93.         interrupt  used  is  10(hex) which is  the  general  monitor
  94.         interrupt.  This particular service is number 2 of about  10
  95.         different  monitor  services available.   This  function  is
  96.         included here as another example to you.
  97.  
  98.              The  next  function,  service  number  6  of  interrupt
  99.         10(hex)  is the window scroll service.   It should  be  self
  100.         explanatory.
  101.  
  102.              In this program, the cursor is positioned and some data
  103.         is  output  to the monitor,  then the cursor is "hidden"  by
  104.         moving  it  to line 26 which is not  displayed.   After  you
  105.         compile and run the program, you will notice that the cursor
  106.         is  not  visible on the monitor.   This is possible  in  any
  107.         program,  but  be  sure  to put the cursor  in  view  before
  108.         returning  to  DOS  because  DOS does not  like  to  have  a
  109.         "hidden" cursor and may do some strange things.
  110.  
  111.              Some time spent studying this program will be  valuable
  112.         to  you as it will reveal how the keyboard data is input  to
  113.         the  computer.   Especially of importance is how the special
  114.         keys such as function keys, arrows, etc. are handled.   Also
  115.         note that this program uses full prototype checking and is a
  116.         good  example  of  how to use it.  Since it  also  uses  the
  117.         "modern"  method  of  function definitions,  it  is  a  good
  118.         example of that also.
  119.  
  120.  
  121.  
  122.  
  123.                                   Page 98
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                      Chapter 14 - Example Programs
  134.  
  135.  
  136.                   WHATNEXT.C - The Batch File Interrogator
  137.  
  138.              This  is  an  example of how to read the  data  on  the
  139.         command line following the function call.  Notice that there
  140.         are  two variables listed within the  parentheses  following
  141.         the main() call.   The first variable is a count of words in
  142.         the entire command line including the command itself and the
  143.         second  variable  is  a  pointer to  an  array  of  pointers
  144.         defining the actual words on the command line.
  145.  
  146.              First the question on the command line, made up of some
  147.         number of words, is displayed on the monitor and the program
  148.         waits for the operator to hit a key.   If the key hit is one
  149.         of  those  in the last "word" of the group of words  on  the
  150.         command  line,  the number of the character within the group
  151.         is  returned to the program where it can be tested with  the
  152.         "errorlevel" command in the batch file.   You could use this
  153.         technique  to  create a variable AUTOEXEC.BAT  file  or  any
  154.         other  batch  file  can  use this for  a  many  way  branch.
  155.         Compile  and  run this file with TEST.BAT for an example  of
  156.         how  it  works in practice.   You may  find  this  technique
  157.         useful  in  one  of  your batch files and  you  will  almost
  158.         certainly  need  to  read in  the  command  line  parameters
  159.         someday.
  160.  
  161.              An  interesting alternative would be for you to write a
  162.         program  named "WOULD.C" that would return a 1 if a  "Y"  or
  163.         "y"  were typed and a zero if any other key were hit.   Then
  164.         your batch file could have a line such as;
  165.  
  166.         WOULD YOU LIKE TO USE THE ALTERNATIVE METHOD (Y/N)
  167.  
  168.              Dos would use "WOULD" as the program name,  ignore  the
  169.         rest  of  the  statement  except for displaying  it  on  the
  170.         screen.   You  would  then respond to the  question  on  the
  171.         monitor  with a single keyhit.   Your batch file would  then
  172.         respond   to  the  1  or  0  returned  and  either  run  the
  173.         alternative  part  of  the batch file or  the  primary  part
  174.         whatever each part was.
  175.  
  176.         WOULD YOU LIKE PRIMARY (Y/N)
  177.         IF ERRORLEVEL 1 GOTO PRIMARY
  178.         (secondary commands)
  179.         GOTO DONE
  180.         :PRIMARY
  181.         (primary commands)
  182.         :DONE
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.                                   Page 99
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                      Chapter 14 - Example Programs
  200.  
  201.  
  202.                         LIST.C - The Program Lister
  203.  
  204.              This program is actually composed of two files,  LIST.C
  205.         and  LISTF.C  that must be separately  compiled  and  linked
  206.         together  with your linker.   There is nothing new here  and
  207.         you  should  have  no  trouble compiling  and  linking  this
  208.         program  by  reading the documentation  supplied  with  your
  209.         Turbo C compiler.
  210.  
  211.              A  LIST.PRJ file is included on the SOURCE disk of  the
  212.         tutorial  as  an aid to you in compiling  and  linking  this
  213.         program.   Read pages 62 and 63 of the Turbo C  Users  Guide
  214.         for instructions on how to do it.
  215.  
  216.              The  only  thing  that is new in this  program  is  the
  217.         inclusion   of  three  "extern"  variables  in  the  LISTF.C
  218.         listing.   The only purpose for this is to tie these  global
  219.         variables  to  the main program and tell the  compiler  that
  220.         these  are not new variables.   The compiler will  therefore
  221.         not  generate any new storage space for them but simply  use
  222.         their names during the compile process.   At link time,  the
  223.         linker  will  get  their actual storage locations  from  the
  224.         LIST.OBJ  file and use those locations for the variables  in
  225.         the  LISTF part of the memory map also.   The  variables  of
  226.         those  names in both files are therefore the same  identical
  227.         variables and can be used just as any other global variables
  228.         could be used if both parts of the program were in one file.
  229.  
  230.              There is no reason why the variables couldn't have been
  231.         defined  in the LISTF.C part of the program and declared  as
  232.         "extern"  in the LIST.C part.   Some of the variables  could
  233.         have  been  defined  in one and some in the  other.   It  is
  234.         merely a matter of personal taste.   Carried to an  extreme,
  235.         all of the variables could have been defined in a third file
  236.         and  named "extern" in both of these files.   The third file
  237.         would then be compiled and included in the linking process.
  238.  
  239.              It would be to your advantage to compile, link, and run
  240.         this  program to prepare you for the next program  which  is
  241.         composed of 6 separate files which must all work together.
  242.  
  243.                         VC.C - The Visual Calculator
  244.  
  245.              This  program  finally ties nearly everything  together
  246.         because  it uses nearly every concept covered in the  entire
  247.         tutorial.   It  is so big that I will not even try to  cover
  248.         the finer points of its operation.   Only a few of the  more
  249.         important points will be discussed.
  250.  
  251.              The  first  thing  you  should do  is  go  through  the
  252.         tutorial  for  VC included in the file  VC.DOC.   There  are
  253.  
  254.  
  255.                                   Page 100
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                      Chapter 14 - Example Programs
  266.  
  267.  
  268.         several  dozen  steps  for you to execute,  with  each  step
  269.         illustrating some aspect of the Visual Calculator.  You will
  270.         get  a  good feel for what it is capable of doing  and  make
  271.         your study of the source code very profitable.  In addition,
  272.         you  will  probably  find  many  ways  to  use  the   Visual
  273.         Calculator  to  solve problems involving calculations  where
  274.         the  simplicity  of  the problem at hand  does  not  warrant
  275.         writing a program.
  276.  
  277.              Notice that the structure definitions,  used in all  of
  278.         the  separate parts of the program,  are defined in the file
  279.         STRUCT.DEF.   During  program development,  when  it  became
  280.         necessary  to change one of the structures slightly,  it was
  281.         not  necessary to change it in all of the  files,  only  one
  282.         file  required modification which was then "included" in the
  283.         source files.   Notice that the transcript data is stored in
  284.         a doubly linked list with the data itself being stored in  a
  285.         separate  dynamically allocated char string.   This line  is
  286.         pointed to by the pointer "lineloc".
  287.  
  288.              For  ease  of development,  the similar functions  were
  289.         grouped together and compiled separately.   Thus, all of the
  290.         functions  involving the monitor were included in  the  file
  291.         named  VIDEO.C,  and all of the functions involving the data
  292.         storage were grouped into the FILE.C  collection.   Dividing
  293.         your  program  in  a  way similar to  this  should  simplify
  294.         debugging and future modifications.
  295.  
  296.              Of special interest is the "monitor()" function.   This
  297.         function  examines  the  video mode through  use  of  a  DOS
  298.         command  and  if it is a 7,  it assumes it is  a  monochrome
  299.         monitor,  otherwise it assumes a color monitor.   The colors
  300.         of  the various fields are established at this time and used
  301.         throughout  the  program.   Most  of  the  data  is  written
  302.         directly  to the video memory,  but some is written  through
  303.         the standard BIOS routines.
  304.  
  305.              The file DEFIN.H is a catalogue of the functions to aid
  306.         in finding the functions.  This file was generated as one of
  307.         the  first  files  and was maintained and  updated  for  use
  308.         during  the  entire  design and coding  lifetime.   It  also
  309.         contains all of the prototype definitions for the  functions
  310.         in  all  of  the source files, and is  "included"  in  every
  311.         source file to do prototype checking.
  312.  
  313.              The  file  VC.PRJ  is included as an  aid  for  you  to
  314.         compile  and  link this program.  The Visual  Calculator  is
  315.         supplied  to you as VC.EXE already compiled and  linked  for
  316.         you.  Feel free, after understanding this code, to modify it
  317.         in any way you desire for your own use.
  318.  
  319.  
  320.  
  321.                                   Page 101
  322.